package com.example.studentalarm.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.studentalarm.R;
import com.example.studentalarm.bean.TimetableBean;
import com.example.studentalarm.utils.TimeUtils;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 *  *课表监听器
 */
public class TimetableAdapter extends RecyclerView.Adapter<TimetableAdapter.ViewHolder> {
    private List<TimetableBean> mData = new ArrayList<>();
    private LayoutInflater mLayoutInflater;
    private OnItemClickListener mOnItemClickListener;

    public TimetableAdapter(Context mContext) {
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(View v, int position);

        void deleteTimetable(TimetableBean bean);
    }

    public List<TimetableBean> getmData() {
        return mData;
    }

    public void bindDataList(List<TimetableBean> dataList) {
        mData.clear();
        mData.addAll(dataList);
        notifyDataSetChanged();
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = mLayoutInflater.inflate(R.layout.timetable_layout_item, parent, false);
        ViewHolder viewHolder = new ViewHolder(v);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        TimetableBean bean = mData.get(position);
        holder.tvTimeTableName.setText(bean.getName());
        Calendar mCalendar = Calendar.getInstance();
        long currentTime = bean.getTime().getTime();
        mCalendar.setTimeInMillis(currentTime);
        int apm = mCalendar.get(Calendar.AM_PM);
        StringBuilder stringBuilder = new StringBuilder(TimeUtils.getDate("yyyy年MM月dd日 EEEE", currentTime));
        if (apm == 0) {
            stringBuilder.append(" 上午 ");
        } else {
            stringBuilder.append(" 下午 ");
        }
        stringBuilder.append(TimeUtils.getDate("HH时mm分", currentTime));
        holder.tvDate.setText(stringBuilder.toString());
        holder.tvAddress.setText(bean.getAddress());
    }

    @Override
    public int getItemCount() {
        return null == mData ? 0 : mData.size();
    }


    public class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.linearParent)
        LinearLayout linearParent;
        @BindView(R.id.tvTimeTableName)
        TextView tvTimeTableName;
        @BindView(R.id.tvDate)
        TextView tvDate;
        @BindView(R.id.tvAddress)
        TextView tvAddress;
        @BindView(R.id.delete)
        Button btnDelete;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            linearParent.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int position = getAdapterPosition();
                    mOnItemClickListener.onItemClick(view, position);
                }
            });
            btnDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int position = getAdapterPosition();
                    TimetableBean bean = mData.get(position);
                    mOnItemClickListener.deleteTimetable(bean);
                }
            });
        }
    }


}
